package com.example.fszq.volleydemo1.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.example.fszq.volleydemo1.MainActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * Created by fszq on 2016/1/7.
 */
public class MyUtil {
    private String returnContent;
    private int saveResult = 0;
    private Context ctx;

    public MyUtil(Context context) {
        this.ctx = context;
    }

    public int saveShared(String key,String content) {
        SharedPreferences.Editor editor = ctx.getSharedPreferences("data",Context.MODE_PRIVATE).edit();
        editor.putString(key,content);
        editor.commit();
        return 1;
    }

    public String getShared(String key) {
        SharedPreferences pref = ctx.getSharedPreferences("data",Context.MODE_PRIVATE);
        return pref.getString(key,"");
    }

    public int saveFile(String filename,String content) {
        FileOutputStream out = null;
        BufferedWriter writer = null;

        try {
            out = ctx.openFileOutput("data",ctx.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out,"GBK"));
            writer.write(content);
        } catch(Exception e) {
            e.printStackTrace();
            Log.d("fileerror", e.getMessage());
            return 0;
        } finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
                Log.d("fileerror", e.getMessage());
                return 0;
            }
        }
        return 1;
    }

    public  String readAssetsFile(String filename) {
        try {
            BufferedReader reader = null;
            StringBuilder content = new StringBuilder();
            InputStream in = ctx.getResources().getAssets().open(filename);
            reader = new BufferedReader(new InputStreamReader(in,"GBK"));
            String line = "";
            while ((line = reader.readLine()) != null) {

                content.append(line);
            }
            reader.close();
            in.close();
            //Log.d("test", content.toString());
            returnContent = content.toString();
        } catch (Exception e) {
            Log.e("TAG", e.getMessage(), e);
            e.printStackTrace();
            //Log.d("filereaderror", e.getMessage());
            returnContent = "";
        }
        return returnContent;
    }

    public String readFile(String fileName) {
        try {
            BufferedReader reader = null;
            StringBuilder content = new StringBuilder();
            InputStream in = ctx.openFileInput(fileName);
            reader = new BufferedReader(new InputStreamReader(in,"GBK"));
            String line = "";
            while ((line = reader.readLine()) != null) {

                content.append(line);
            }
            reader.close();
            in.close();

        } catch (Exception e) {
            Log.e("TAG", e.getMessage(), e);
            e.printStackTrace();
            //Log.d("filereaderror", e.getMessage());
            returnContent = "";

        }
        return  returnContent;
    }
}
